/*
 *  CETreeListItem.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/controls/CETreeListItem.h"

#include "cocos2dx_extentions/controls/CEListView.h"

using namespace cocos2d;

namespace cocosext {

/////////////////////////////////////////////////////
//
/** CETreeListItem 
* 
*/
/////////////////////////////////////////////////////

CETreeListItem * CETreeListItem::itemFromNormalSprite(const CETreeListItemInfo& info, CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
{
    CETreeListItem *pRet = new CETreeListItem(info);
	CCAssert(pRet != NULL, NULL);
	if (pRet->initFromNormalSprite(normalSprite, selectedSprite, disabledSprite))
	{
		pRet->autorelease();
        return pRet;
	}
	else
	{
		CC_SAFE_DELETE(pRet)
		return NULL;
	}
}

/////////////////////////////////////////////////////
//
  
bool CETreeListItem::initFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
{
	if (!CEListItem::initFromNormalSprite(normalSprite, selectedSprite, disabledSprite, NULL, NULL))
		return false;


	return true;
}

/////////////////////////////////////////////////////
// public Cocos2d interface

void CETreeListItem::activate()
{
	if (m_bIsEnabled)
		revertExpandState();
}

/////////////////////////////////////////////////////
// Main interface
void CETreeListItem::revertExpandState()
{
	_isExpanded = !_isExpanded;
	if (_isExpanded)
		_list->insertListItemsAfter(_subitemsVec, this);
	else
		_list->removeListItems(_subitemsVec);
}

/////////////////////////////////////////////////////
// Protected interface
void CETreeListItem::onAfterAdd(CEVerticalListView* list)
{
	CEListItem::onAfterAdd(list);
	
	if (_isExpanded)
		_list->insertListItemsAfter(_subitemsVec, this);
}

void CETreeListItem::onBeforeRemove()
{
	if (_isExpanded)
		_list->removeListItems(_subitemsVec);

	CEListItem::onBeforeRemove();
}


/////////////////////////////////////////////////////
//
/** CETreeListItemSupplement 
* 
*/
/////////////////////////////////////////////////////

CETreeListItemSupplement * CETreeListItemSupplement::itemFromNormalSprite(const CEListItemInfo& info, CETreeListItem* master, CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
{
    CETreeListItemSupplement *pRet = new CETreeListItemSupplement(info, master);
	CCAssert(pRet != NULL, NULL);
	if (pRet->initFromNormalSprite(normalSprite, selectedSprite, disabledSprite))
	{
		pRet->autorelease();
        return pRet;
	}
	else
	{
		CC_SAFE_DELETE(pRet)
		return NULL;
	}
}

/////////////////////////////////////////////////////
//
  
bool CETreeListItemSupplement::initFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
{
	if (!CEListItem::initFromNormalSprite(normalSprite, selectedSprite, disabledSprite, NULL, NULL))
		return false;


	return true;
}

/////////////////////////////////////////////////////
// public Cocos2d interface

void CETreeListItemSupplement::activate()
{
	if (m_bIsEnabled)
		_master->revertExpandState();
}

}; // end namespace cocosext

